#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->loadFontList();
    sansModel.setStringList(this->sans);
    this->ui->listViewSans->setModel(&sansModel);
    serifModel.setStringList(this->serif);
    this->ui->listViewSerif->setModel(&serifModel);
    monoModel.setStringList(this->mono);
    this->ui->listViewMono->setModel(&monoModel);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::loadFontList()
{
    QProcess proc;
    proc.start("fc-list");  //執行fc-list外部指令
    //等待指令執行完成
    while (!proc.waitForFinished(300))
    {
        qApp->processEvents();
    }
    QString fonts = QString::fromLocal8Bit(proc.readAllStandardOutput()); //獲取全部輸出
    QStringList fontlist = fonts.split(QChar('\n'));  //按行切分字體
    for (int i = 0;i< fontlist.count();i++)    //獲取字體真正的名稱
    {
        fontlist[i] = fontlist[i].split(QChar(':')).at(0);
        fontlist[i] = fontlist[i].split(QChar(',')).at(0);
    }
    fontlist.sort();  //排序字體
    for (int i = 0;i<fontlist.count();i++)  //迴圈刪除重複字體
    {
        while (true)
        {
            if (i == fontlist.count() - 1) break;
            if (fontlist[i]==fontlist[i+1])
            {
                fontlist.removeAt(i+1);
            }
            else
            {
                break;
            }
        }
    }
    for (int i = 0;i< fontlist.count();i++)  //將字體們添加到介面中
    {
        this->ui->listWidget->addItem(fontlist[i]);
    }
}

void MainWindow::on_btnAddToSans_clicked()
        //添加到sans
{
    QString fontName = this->ui->listWidget->selectedItems()[0]->text();
    this->sans.append(fontName);
    this->sansModel.insertRow(0);
    this->sansModel.setData(this->sansModel.index(0),fontName);
    //this->sansModel.setStringList(this->sans);
}

void MainWindow::on_btnAddToSerif_clicked()
        //添加到serif
{
    QString fontName = this->ui->listWidget->selectedItems()[0]->text();
    this->serifModel.insertRow(0);
    this->serifModel.setData(this->serifModel.index(0),fontName);
}

void MainWindow::on_btnAddToMono_clicked()
        //添加到mono
{
    QString fontName = ui->listWidget->selectedItems()[0]->text();
    monoModel.insertRow(0);
    monoModel.setData(this->monoModel.index(0),fontName);
}

void MainWindow::on_btnSansUp_clicked()
{
    QModelIndex current = this->ui->listViewSans->currentIndex();  //被選中的項目
    if (current.row()==0){  //如果是第一個就退出
        return;
    }
    QString strNow = current.data().value<QString>();  //被選中的值
    QModelIndex above = this->sansModel.index(current.row()-1);   //上一個
    QString strAbove = above.data().value<QString>();  //上一個的值
    // 交換被選中和被選中的上一個的值
    this->sansModel.setData(above,strNow);
    this->sansModel.setData(current,strAbove);
    this->ui->listViewSans->setCurrentIndex(above);  //選中上一個
}

void MainWindow::on_btnSerifUp_clicked()
{
    QModelIndex current = this->ui->listViewSerif->currentIndex();  //被選中的項目
    if (current.row()==0){  //如果是第一個就退出
        return;
    }
    QString strNow = current.data().value<QString>();  //被選中的值
    QModelIndex above = this->serifModel.index(current.row()-1);   //上一個
    QString strAbove = above.data().value<QString>();  //上一個的值
    // 交換被選中和被選中的上一個的值
    this->serifModel.setData(above,strNow);
    this->serifModel.setData(current,strAbove);
    this->ui->listViewSerif->setCurrentIndex(above);  //選中上一個
}

void MainWindow::on_btnMonoUp_clicked()
{
    QModelIndex current = this->ui->listViewMono->currentIndex();  //被選中的項目
    if (current.row()==0){  //如果是第一個就退出
        return;
    }
    QString strNow = current.data().value<QString>();  //被選中的值
    QModelIndex above = this->monoModel.index(current.row()-1);   //上一個
    QString strAbove = above.data().value<QString>();  //上一個的值
    // 交換被選中和被選中的上一個的值
    this->monoModel.setData(above,strNow);
    this->monoModel.setData(current,strAbove);
    this->ui->listViewMono->setCurrentIndex(above);  //選中上一個
}

void MainWindow::on_btnSansDown_clicked()
{
    QModelIndex current = this->ui->listViewSans->currentIndex();   //被選中的項目
    if (current.row()==this->sansModel.stringList().length()-1)  //如果是最後一個就退出
    {
        return;
    }
    QString strNow = current.data().value<QString>();   //被選中的值
    QModelIndex next = this->sansModel.index(current.row()+1);  //下一個
    QString strNext = next.data().value<QString>();  //下一個的值
    // 交換被選中和下一個的值
    this->sansModel.setData(next,strNow);
    this->sansModel.setData(current,strNext);
    this->ui->listViewSans->setCurrentIndex(next);
}

void MainWindow::on_btnSerifDown_clicked()
{
    QModelIndex current = this->ui->listViewSerif->currentIndex();   //被選中的項目
    if (current.row()==this->serifModel.stringList().length()-1)  //如果是最後一個就退出
    {
        return;
    }
    QString strNow = current.data().value<QString>();   //被選中的值
    QModelIndex next = this->serifModel.index(current.row()+1);  //下一個
    QString strNext = next.data().value<QString>();  //下一個的值
    // 交換被選中和下一個的值
    this->serifModel.setData(next,strNow);
    this->serifModel.setData(current,strNext);
    this->ui->listViewSerif->setCurrentIndex(next);
}

void MainWindow::on_btnMonoDown_clicked()
{
    QModelIndex current = this->ui->listViewMono->currentIndex();   //被選中的項目
    if (current.row()==this->monoModel.stringList().length()-1)  //如果是最後一個就退出
    {
        return;
    }
    QString strNow = current.data().value<QString>();   //被選中的值
    QModelIndex next = this->monoModel.index(current.row()+1);  //下一個
    QString strNext = next.data().value<QString>();  //下一個的值
    // 交換被選中和下一個的值
    this->monoModel.setData(next,strNow);
    this->monoModel.setData(current,strNext);
    this->ui->listViewMono->setCurrentIndex(next);
}


void MainWindow::on_btnDelFromSans_clicked()
{
    this->sansModel.removeRow(this->ui->listViewSans->currentIndex().row());
}

void MainWindow::on_btnDelFromSerif_clicked()
{
    this->serifModel.removeRow(this->ui->listViewSerif->currentIndex().row());
}



void MainWindow::on_btnDelFromMono_clicked()
{
    this->monoModel.removeRow(this->ui->listViewMono->currentIndex().row());
}



void MainWindow::on_btnSave_clicked()
{
    QString fileName = QFileDialog::getSaveFileName(this,
                                 QString("保存「.fonts.conf」設定檔"),
                                 QString("$HOME/"));
    if (fileName != NULL)
    {
        QFile file(fileName);
        file.open(QIODevice::WriteOnly);
        QTextStream fout(&file);
            fout<<"<?xml version=\"1.0\"?>\n"
                    <<"<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n"
                    <<"<fontconfig>\n"
                    <<"<match>\n"
                    <<"\t<test name=\"family\"><string>sans-serif</string></test>\n"
                    <<"\t<edit name=\"family\" mode=\"prepend\" binding=\"strong\">\n";
            for (int i=0;i < this->sansModel.stringList().length();i++)
            {
                fout<<"\t\t<string>"<<this->sansModel.stringList()[i]<<"</string>\n";
            }
            fout<<"\t</edit>\n";
            fout<<"</match>\n";
            fout<<"<match>\n"
                    <<"\t<test name=\"family\"><string>serif</string></test>\n"
                    <<"\t<edit name=\"family\" mode=\"prepend\" binding=\"strong\">";
            for (int i=0;i < this->serifModel.stringList().length();i++)
            {
                fout<<"\t\t<string>"<<this->serifModel.stringList()[i]<<"</string>\n";
            }
            fout<<"\t</edit>\n";
            fout<<"</match>\n";

            fout<<"<match>\n"
                    <<"\t<test name=\"family\"><string>monospace</string></test>\n"
                    <<"\t<edit name=\"family\" mode=\"prepend\" binding=\"strong\">";
            for (int i=0;i < this->monoModel.stringList().length();i++)
            {
                fout<<"\t\t<string>"<<this->monoModel.stringList()[i]<<"</string>\n";
            }
            fout<<"\t</edit>\n";
            fout<<"</match>\n";
            fout<<"</fontconfig>\n";
            file.close();
    }
}
